﻿@namespace SageKing.Studio.Pages.DictType.component

@implements IAddEdit<SysDictData,long>

@inject SysDictDataService _dataService
@inject SelectEnumCacheService<DBTypesEnum> selectCacheService

<Form @ref="form"
      Model="@model" ValidateMode="FormValidateMode.Complex"
      ValidateOnChange="true"
      LabelColSpan="8" WrapperColSpan="16">

    <Row Gutter="24">
        <Col Span="24">
        <FormItem Label="名称" LabelColSpan="4" WrapperColSpan="20">
            <Input ShowCount MaxLength="64" Width="100%" @bind-Value="@context.Name" />
        </FormItem>
        </Col>
    </Row>
    <Row Gutter="24">
        <Col Span="24">
        <FormItem Label="编码" LabelColSpan="4" WrapperColSpan="20">
            <Input ShowCount MaxLength="64" Width="100%" @bind-Value="@context.Code" />
        </FormItem>
        </Col>
    </Row>
    <Row Gutter="24">
        <Col Span="12">
        <FormItem Label="值">
            <Input ShowCount MaxLength="64" Width="100%" @bind-Value="@context.Value" />
        </FormItem>
        </Col>
        <Col Span="12">
            @if (IsDB == DictTypeEnum.DB)
            {
            <FormItem Label="DB类型">
                <Select @bind-Value="@context.ValueInt"
                        DataSource="@SelectType"
                        DefaultValue="0"
                        LabelName="@nameof(KeyValue<string, int>.Key)"
                        ValueName="@nameof(KeyValue<string, int>.Value)"
                        Style="width: 100%;"
                        TItemValue="int"
                        TItem="KeyValue<string, int>"
                        Placeholder="请选数据库类型"
                        DefaultActiveFirstOption="true" OnSelectedItemChanged="handleChange"
                        EnableSearch>
                </Select>
            </FormItem>

            }
            else
            {
            <FormItem Label="值int">
                <AntDesign.InputNumber @bind-Value="@context.ValueInt" Min="1" Max="999999999" Width="100%" PlaceHolder="值int"></AntDesign.InputNumber>
            </FormItem>
            }
        </Col>
    </Row>
    <Row Gutter="24">
        <Col Span="12">
        <FormItem Label="是否启用">
            <Switch @bind-Value="@context.Status" CheckedChildren="开" UnCheckedChildren="关"></Switch>
        </FormItem>
        </Col>
        <Col Span="12">
        <FormItem Label="排序">
            <AntDesign.InputNumber @bind-Value="@context.OrderNo" Min="1" Max="9999" DefaultValue="100" Width="100%" PlaceHolder="排序"></AntDesign.InputNumber>
        </FormItem>
        </Col>
    </Row>
    @*     <Row Gutter="24">
    <Col Span="24">
    <FormItem Label="显示样式-Style" LabelColSpan="4" WrapperColSpan="20">
    <TextArea ShowCount MaxLength="512" Width="100%" Placeholder="显示样式-Style(控制显示样式)" @bind-Value="@context.StyleSetting"></TextArea>
    </FormItem>
    </Col>
    </Row>
    <Row Gutter="24">
    <Col Span="24">
    <FormItem Label="显示样式-Class" LabelColSpan="4" WrapperColSpan="20">
    <TextArea ShowCount MaxLength="512" Width="100%" Placeholder="显示样式-Class(控制显示样式)" @bind-Value="@context.ClassSetting"></TextArea>
    </FormItem>
    </Col>
    </Row> *@
    <Row Gutter="24">
        <Col Span="24">
        <FormItem Label="拓展数据" LabelColSpan="4" WrapperColSpan="20">
            <TextArea ShowCount MaxLength="512" Width="100%" Placeholder="拓展数据(保存业务功能的配置项)" @bind-Value="@context.ExtData"></TextArea>
        </FormItem>
        </Col>
    </Row>
    <Row Gutter="24">
        <Col Span="24">
        <FormItem Label="备注" LabelColSpan="4" WrapperColSpan="20">
            <TextArea ShowCount MaxLength="200" Width="100%" Placeholder="备注" @bind-Value="@context.Remark"></TextArea>
        </FormItem>
        </Col>
    </Row>
</Form>

@code {

    [Parameter]
    public DictTypeEnum IsDB { get; set; } = DictTypeEnum.None;

    //select data
    IList<KeyValue<string, int>> SelectType;

    #region IAddEdit
    public IAddEdit<SysDictData, long> Me { get { return this; } }

    public bool IsVersionValidation { get; set; } = false;

    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public SysDictData model { get; set; }

    public IBaseService<SysDictData, long> DataService
    { get { return _dataService; } }

    public AntDesign.Form<SysDictData> form { get; set; }
    #endregion

    bool loading;

    void handleChange(KeyValue<string, int> db)
    {
        model.Code = db.Key;       
        Console.WriteLine(db);
    }

    public Action<SysDictData,ChangedEnum> OnModelChanged{ get; set; }

    protected override async Task OnInitializedAsync()
    {
        var getAll = selectCacheService.GetCache(0, (int)DBTypesEnum.PostgreSQL.GetMax(), true);
        SelectType = getAll.Select(a => new KeyValue<string, int>(a.Key, (int)a.Value)).ToList();
    }
}